package cn.ifafu.ifafu.util

import android.app.Application
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import androidx.core.net.toUri
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.blankj.utilcode.util.ToastUtils


//跟App相关的辅助类
object AppUtils {

    private lateinit var application: Application

    fun init(app: Application) {
        application = app
    }

    /**
     * 复制文本到剪贴板
     * @param text 复制的内容
     * @param label 复制的内容的标签
     */
    fun copyToClipboard(text: String, label: String = text) {
        val cm = application.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText(label, text)
        cm.setPrimaryClip(clipData)
    }

    /**
     * 跳转到QQ群
     * @param key QQ群的key,不是群号,前往https://qun.qq.com获取
     */
    fun jumpToQQGroup(key: String) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.action = "android.intent.action.VIEW"
        intent.setData(("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D$key").toUri())
        try {
            startActivity(intent)
        } catch (e: Exception) {
            ToastUtils.showShort("您还没有安装QQ，请先安装软件")
        }
    }
}